﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Configuration;
using Barbar.UrlGuard.Dal.Persist;
using Barbar.UrlGuard.Dal;
using System.Threading;
using Barbar.UrlGuard.BL;
using Barbar.UrlGuard.BL.Concrete;
using System.Globalization;

namespace Barbar.UrlGuard {
  static class Program {
    static void Configure() {
      ServiceLocator.Instance.AddSingleton<IAutoStartupService, AutoStartupService>();
      ServiceLocator.Instance.AddSingleton<IEmailAlertService, EmailAlertService>();
      ServiceLocator.Instance.AddSingleton<ISymmetricPasswordService, SymmetricPasswordService>();
      ServiceLocator.Instance.AddSingleton<ITickService, TickService>();
      ServiceLocator.Instance.AddSingleton<ILanguageSchemaProvider, LanguageSchemaProvider>();
      ServiceLocator.Instance.AddSingleton<IMessageCollectionProvider, MessageCollectionProvider>();
      ServiceLocator.Instance.AddSingleton<ITranslator, Translator>();
      ServiceLocator.Instance.AddSingleton<IInjectorUtility, InjectorUtility>();
      ServiceLocator.Instance.AddSingleton<IBrowserProvider, BrowserProvider>();
      ServiceLocator.Instance.AddSingleton<IDataContextHolder, DataContextHolder>();
      ServiceLocator.Instance.AddSingleton<IDataContextUpdator, DataContextUpdator>();
      ServiceLocator.Instance.AddSingleton<IHistoryPersistor, BinaryHistoryPersistor>();
      ServiceLocator.Instance.AddSingleton<IBinaryFactory<MonitoringCheck>, BinaryFactory>();
      ServiceLocator.Instance.AddSingleton<IBinaryFactory<IndexEntry>, BinaryFactory>();
      ServiceLocator.Instance.AddSingleton<IBinaryFactory<RecordHeader>, BinaryFactory>();
      ServiceLocator.Instance.AddSingleton<ILanguageObserver, LanguageObserver>();
      ServiceLocator.Instance.AddSingleton<IWebRequestUtility, WebRequestUtility>();
      ServiceLocator.Instance.AddSingleton<ILogger, Logger>();
      ServiceLocator.Instance.AddSingleton<IGraphProvider, GraphProvider>();
    }

      // STA model required for things like clipboard accesss
    [STAThread]
    static void Main() {
      Configure();

      IDataContextHolder holder = ServiceLocator.Instance.Resolve<IDataContextHolder>();
      int languageID = holder.Context.LanguageID;
      CultureInfo culture = new CultureInfo(languageID);
      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;

      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
      AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
      Application.Run(new FormMain());
    }

    static void HandleError(Exception exception) {
      LanguageFileException languageException = null;
      Exception innerException = exception;
      while (innerException != null) {
        languageException = innerException as LanguageFileException;
        if (languageException != null) {
          break;
        }
        innerException = innerException.InnerException;
      }

      if (languageException != null) {
        exception = languageException;
      }

      FormError formError = new FormError(exception);
      formError.ShowDialog();
      Environment.Exit(-1);
    }

    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) {
      HandleError(e.Exception);
    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        if (e.ExceptionObject is Exception)
        {
            HandleError(e.ExceptionObject as Exception);
        }
        else
        {
            HandleError(new Exception("Unknown Exception"));
        }
    }

  }
}
